package com.htthorizon.model.service;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.SM2;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import com.htthorizon.common.core.context.SecurityContextHolder;
import com.htthorizon.common.core.exception.ServiceException;
import com.htthorizon.common.core.web.page.TableDataInfo;
import com.htthorizon.model.domain.ModelNeed;
import com.htthorizon.model.enums.NeedStatus;
import com.htthorizon.model.mapper.ModelNeedMapper;
import com.htthorizon.model.request.ModelNeedAddReq;
import com.htthorizon.model.request.ModelNeedSearchReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author Brilliant
 */
@Service
public class ModelNeedServiceImpl implements IModelNeedService{
    @Autowired
    private ModelNeedMapper modelNeedMapper;

    @Override
    public TableDataInfo selectModelNeedList(ModelNeedSearchReq modelNeed, TableDataInfo tableDataInfo, Integer pageNum, Integer pageSize) {
     long count=   modelNeedMapper.count(modelNeed);
        List<ModelNeed> collect = modelNeedMapper.getModelNeedList(modelNeed,(pageNum-1)*pageSize,pageSize).stream()
                .map(ModelNeedServiceImpl::map)
                .collect(Collectors.toList());
        tableDataInfo.setRows(collect);
        tableDataInfo.setTotal(count);
        tableDataInfo.setCode(200);
        return tableDataInfo;
    }
    private static ModelNeed map(ModelNeed modelNeed){
//        if (!Objects.isNull(modelNeed.getModelAuthorPhone())) {
//            modelNeed.setModelAuthorPhone(Base64.decodeStr(modelNeed.getModelAuthorPhone()));
//        }
        return modelNeed;
    }

    @Override
    public boolean addModelNeed(ModelNeedAddReq modelNeedAddReq) throws UnsupportedEncodingException {
        modelNeedAddReq.setStatus(NeedStatus.CECKING.getValue());
        Long userId = SecurityContextHolder.getUserId();
        String userName = SecurityContextHolder.getUserName();
        DateTime date = DateUtil.date();
        modelNeedAddReq.setCreateBy(userName);
        modelNeedAddReq.setCreateId(userId);
        modelNeedAddReq.setCreateTime(date);
        if (!Objects.isNull(modelNeedAddReq.getModelAuthorPhone())){
            modelNeedAddReq.setModelAuthorPhone(Base64.encode(modelNeedAddReq.getModelAuthorPhone()));
        }
        return modelNeedMapper.addModelNeed(modelNeedAddReq);
    }

    public static void main(String[] args) {
        String text="15576612930";
        SM2 sm2 = SmUtil.sm2();
        String encryptStr = sm2.encryptBcd(text, KeyType.PublicKey);
        String decryptStr = StrUtil.utf8Str(sm2.decryptFromBcd(encryptStr, KeyType.PrivateKey));
        System.out.println(encryptStr);
        System.out.println(decryptStr);
    }

    @Override
    public boolean edit(ModelNeedAddReq modelNeedAddReq) {
        return modelNeedMapper.updateById(modelNeedAddReq);
    }

    @Override
    public boolean delById(Long id) {
        ModelNeed modelNeed = modelNeedMapper.selectByPrimaryKey(id);
        if (NeedStatus.CECKED.getValue().equals(modelNeed.getStatus())
            || NeedStatus.NO_CECKED.getValue().equals(modelNeed.getStatus())) {
            throw new ServiceException("不能删除已处理需求");
        }
        modelNeedMapper.delById(id);
        return true;
    }

    @Override
    public boolean checkStatus(Long id,Integer checkStatus) {
        ModelNeedAddReq modelNeed = new ModelNeedAddReq();
        modelNeed.setId(id);
        modelNeed.setStatus(checkStatus);
        ModelNeed modelNeed2 = modelNeedMapper.getById(id);
        modelNeed.setCheckTime(DateUtil.date());
//        if (modelNeed2.getStatus().equals(4)){
//            throw new RuntimeException("不能操作");
//        }
        return modelNeedMapper.updateById(modelNeed);
    }
}
